<template>
  <el-dialog
    :title="`${drawerProps.title}业务模型属性`"
    v-model="drawerVisible"
    :draggable="true"
    :overflow="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    top="30px"
    width="800px"
    :destroy-on-close="true"
  >
    <el-form
      ref="ruleFormRef"
      style="height: 480px; overflow: hidden auto"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-row>
        <form-item title="业务模型" prop="businessModelCnName" :title-span="4" :item-span="19">
          <el-input v-model="drawerProps.row!.businessModelCnName" clearable></el-input>
        </form-item>

        <form-item title="中文名称" prop="cnName" :title-span="4" :item-span="19">
          <el-input v-model="drawerProps.row!.cnName" clearable></el-input>
        </form-item>

        <form-item title="英文名称" prop="enName" :title-span="4" :item-span="19">
          <el-input v-model="drawerProps.row!.enName" clearable></el-input>
        </form-item>

        <form-item title="数据元素" prop="dataElementCnName" :title-span="4" :item-span="19">
          <el-input v-model="drawerProps.row!.dataElementCnName" clearable></el-input>
        </form-item>

        <form-item title="属性编号" prop="attributeNum" :title-span="4" :item-span="19">
          <el-input v-model="drawerProps.row!.attributeNum" clearable></el-input>
        </form-item>

        <form-item title="排序" prop="sort" :title-span="4" :item-span="19">
          <el-input-number v-model="drawerProps.row!.sort" :min="0"></el-input-number>
        </form-item>

        <form-item title="数据来源" prop="dataFrom" :title-span="4" :item-span="19">
          <common-select
            data-key="dataFrom"
            :request-api="() => apiService.findClassify('dataFrom')"
            v-model="drawerProps.row!.dataFrom"
          ></common-select>
        </form-item>

        <form-item title="业务领域" prop="businessDomain" :title-span="4" :item-span="19">
          <el-input v-model="drawerProps.row!.businessDomain" clearable></el-input>
        </form-item>

        <form-item title="负责人" prop="ownerUser" :title-span="4" :item-span="19">
          <el-input v-model="drawerProps.row!.ownerUser" clearable></el-input>
        </form-item>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="BusinessModelAttributeDrawer">
import { ref, reactive } from "vue";
import { ElNotification, FormInstance } from "element-plus";
import { Model } from "@/views/model/modeTypes";
import apiService from "@/api/services/apiService";
import formItem from "@/components/application/form/form-item.vue";
import commonSelect from "@/components/SelectFilter/common-select.vue";
import { useUserStore } from "@/stores/modules/user";

const rules = reactive({
  cnName: [{ required: true, message: "请输入中文名称" }],
  enName: [{ required: true, message: "请输入英文名称" }]
});

interface DrawerProps {
  title: string;
  tbName: string;
  isView: boolean;
  row: Partial<Model.BusinessModelAttribute>;
  api?: (tabName: string, params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  tbName: "",
  row: {},
  api: undefined,
  getTableList: undefined
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value?.validate(async valid => {
    if (!valid) return;
    try {
      if (drawerProps.value.title === "新增") {
        // 初始化创建人和创建时间
        drawerProps.value.row.create_at = new Date();
        drawerProps.value.row.create_user = useUserStore().userInfo._id;
      }

      if (drawerProps.value.title === "新增") {
        await apiService.save(drawerProps.value.tbName, drawerProps.value.row);
      } else {
        await apiService.updateById(drawerProps.value.tbName, drawerProps.value.row);
      }
      ElNotification({
        title: "温馨提示",
        message: "保存成功",
        type: "success"
      });
      drawerProps.value.getTableList && drawerProps.value.getTableList();
      drawerVisible.value = false;
    } catch (error) {
      console.error(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
